package ltd.dujiabao.servicebase.exceptionhandler;

import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.commonutils.ExceptionUtil;
import ltd.dujiabao.commonutils.Result;
import ltd.dujiabao.servicebase.exception.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author : DuJiabao
 * @Project : online-course-project
 * @Time : 2021/4/19 10:57
 * @Desc :
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
  /**
   * 全局处理异常的方法
   * @param e
   * @return
   */
  @ExceptionHandler(Exception.class)
  @ResponseBody
  public Result error(Exception e) {
    //e.printStackTrace();
    log.error(ExceptionUtil.getMessage(e));
    return Result.failure().setMessage("发生了" + e.toString());
  }

  @ExceptionHandler(MyException.class)
  @ResponseBody
  public Result myError(MyException e) {
    //e.printStackTrace();
    log.error(ExceptionUtil.getMessage(e));
    return Result.failure().setMessage(e.getMes()).setCode(e.getCode());
  }

}
